//
//  MYVisiterView.swift
//  sinaWeibo
//
//  Created by 于 on 16/8/8.
//  Copyright © 2016年 于. All rights reserved.
//

import UIKit
import SnapKit
class MYVisiterView: UIView {
    
    //闭包传值
     var passValue: (() -> ())?
    
    
 // MARK: 懒加载
    //转动图片
    private lazy var cycleIma: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //图标
    private lazy var picture: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //罩图
    private lazy var makeIma: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //描述
    private lazy var discLab: UILabel = {
    
        let lab = UILabel()
    
        lab.text = "关注一些人，回这里看看有什么惊喜 关注一些人，回这里看看有什么惊喜 "
        lab.font = UIFont.systemFontOfSize(14)
        lab.textColor = UIColor.darkGrayColor()
        lab.textAlignment = .Center
        lab.numberOfLines = 0
        
        return lab
        
    }()
    //登录按钮
    private lazy var loginBtn: UIButton = {
    
    let btn = UIButton()
        
    btn.addTarget(self, action: "login", forControlEvents: .TouchUpInside)
        
    btn.setTitle("登录", forState: .Normal)
    btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
    btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
    btn.titleLabel?.font = UIFont.systemFontOfSize(14)
    btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
    return btn
    
    }()
    
    //注册按钮
    private lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        btn.addTarget(self, action: "register", forControlEvents: .TouchUpInside)
        
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
    
        return btn
        
    }()
    
     // MARK: 初始化
    override init(frame: CGRect) {
        
       super.init(frame: frame)
        
        setUpUI()
       
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

     // MARK: 开启动画
    
   private func animationStart() {
    
        //  创建核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  动画目的地
        animation.toValue = 2 * M_PI
        //  动画时长
        animation.duration = 20
        //  执行次数
        animation.repeatCount = MAXFLOAT
        //  动画执行完成不让其释放
        animation.removedOnCompletion = false
        //  执行动画
        cycleIma.layer.addAnimation(animation, forKey: nil)
    
    
    }
    
    //修改访客视图信息
    
    func visiterViewMessage(imaName: String?, title: String?) {
    
        
        if let mes = title, imaName = imaName {
        
            picture.image = UIImage(named: imaName)
            
            discLab.text = mes
            
            cycleIma.hidden = true
        
        }else {
            
        cycleIma.hidden = false
             animationStart()
        
        }
    
    }
    
    
   // MARK: 设置界面
    func setUpUI() {
        
  //添加子控件
    addSubview(cycleIma)
    addSubview(makeIma)
    addSubview(picture)
    addSubview(discLab)
    addSubview(loginBtn)
    addSubview(registerBtn)
  //添加约束
        
     cycleIma.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(self)
        }
        
     picture.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(self)
        }
        
     makeIma.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(self)
        }
     discLab.snp_makeConstraints { (make) -> Void in
        
        make.top.equalTo(cycleIma.snp_bottom)
        make.centerX.equalTo(cycleIma)
        make.width.equalTo(224)
        }
    
    loginBtn.snp_makeConstraints { (make) -> Void in
        
        make.top.equalTo(discLab.snp_bottom).offset(10)
        make.leading.equalTo(discLab)
        make.width.equalTo(80)
        make.height.equalTo(30)
        
        }
    
    registerBtn.snp_makeConstraints { (make) -> Void in
            
        make.top.equalTo(discLab.snp_bottom).offset(10)
        make.trailing.equalTo(discLab)
        make.width.equalTo(80)
        make.height.equalTo(30)
            
        }
    
}
    
     // MARK: 点击事件
    
    func login() {
    
    passValue?()
        
    }
    
    func register() {
    
    
    passValue?()
    }
    
    deinit {
    
    print("xx")
    }
    

}

 